package com.zyd.recyclerviewdemo;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zyd.recyclerviewdemo.adapter.SimpleRecyclerViewAdapter;
import com.zyd.recyclerviewdemo.view.PullToRefreshView;

import java.util.ArrayList;
import java.util.List;

public class RecyclerPullToRefreshActivity extends AppCompatActivity implements PullToRefreshView.OnFooterRefreshListener, PullToRefreshView.OnHeaderRefreshListener {

    private Toolbar toolbar;
    private List<String> dataList = new ArrayList<>();

    private RecyclerView recyclerView;
    private SimpleRecyclerViewAdapter adapter;
    private PullToRefreshView pull_to_refresh;

    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_pull_refresh);
        init();
    }

    private void init() {
        toolbar = findViewById(R.id.toolbar);
        // 使用Toolbar替换ActionBar
        setSupportActionBar(toolbar);
        recyclerView = findViewById(R.id.recyclerView);
        pull_to_refresh = findViewById(R.id.pull_to_refresh);
        // 模拟获取数据
        getData();

        adapter = new SimpleRecyclerViewAdapter(this, dataList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        pull_to_refresh.setLayoutManager(linearLayoutManager);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);


        pull_to_refresh.setOnFooterRefreshListener(this);
        pull_to_refresh.setOnHeaderRefreshListener(this);
    }

    private void getData() {
        String letter = "Item";
        for (int i = 0; i < 50; i++) {
            dataList.add(letter + i);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.layout_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.liner:
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
                recyclerView.setLayoutManager(linearLayoutManager);
                pull_to_refresh.setLayoutManager(linearLayoutManager);
                break;

            case R.id.grid:
                GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
                recyclerView.setLayoutManager(gridLayoutManager);
                pull_to_refresh.setLayoutManager(gridLayoutManager);
                break;
        }
        dataList.clear();
        getData();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
        Log.w("SwipeRecyclerActivity", "加载完成");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                getData();
                adapter.notifyDataSetChanged();
                pull_to_refresh.onFooterRefreshComplete();
            }
        }, 2000);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
        Log.w("SwipeRecyclerActivity", "刷新完成");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (dataList.size() > 0) {
                    dataList.clear();
                    getData();
                }
                pull_to_refresh.onHeaderRefreshComplete();
            }
        }, 2000);
    }
}